<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="退单号" prop="backSerial">
        <el-input
          v-model="queryParams.backSerial"
          placeholder="请输入退单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="退款金额" prop="refundAmount">
        <el-input
          v-model="queryParams.refundAmount"
          placeholder="请输入退款金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="退单备注" prop="backProductRemarks">
        <el-input
          v-model="queryParams.backProductRemarks"
          placeholder="请输入退单备注"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品编码" prop="backProductCode">
        <el-input
          v-model="queryParams.backProductCode"
          placeholder="请输入商品编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品名称" prop="backProductName">
        <el-input
          v-model="queryParams.backProductName"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品数量" prop="backProductNum">
        <el-input
          v-model="queryParams.backProductNum"
          placeholder="请输入商品数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品主图" prop="backMainPic">
        <el-input
          v-model="queryParams.backMainPic"
          placeholder="请输入商品主图"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品规格" prop="backSpecification">
        <el-input
          v-model="queryParams.backSpecification"
          placeholder="请输入商品规格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="省" prop="province">
        <el-input
          v-model="queryParams.province"
          placeholder="请输入省"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="市" prop="city">
        <el-input
          v-model="queryParams.city"
          placeholder="请输入市"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="县/区" prop="county">
        <el-input
          v-model="queryParams.county"
          placeholder="请输入县/区"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="乡村/街道" prop="village">
        <el-input
          v-model="queryParams.village"
          placeholder="请输入乡村/街道"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="收货人" prop="addressUser">
        <el-input
          v-model="queryParams.addressUser"
          placeholder="请输入收货人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系方式" prop="addressPhone">
        <el-input
          v-model="queryParams.addressPhone"
          placeholder="请输入联系方式"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="crTime">
        <el-date-picker clearable
          v-model="queryParams.crTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="更新时间" prop="upTime">
        <el-date-picker clearable
          v-model="queryParams.upTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择更新时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="备用字段1" prop="backField1">
        <el-input
          v-model="queryParams.backField1"
          placeholder="请输入备用字段1"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="备用字段2" prop="backField2">
        <el-input
          v-model="queryParams.backField2"
          placeholder="请输入备用字段2"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="备用字段3" prop="backField3">
        <el-input
          v-model="queryParams.backField3"
          placeholder="请输入备用字段3"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="备用字段4" prop="backField4">
        <el-input
          v-model="queryParams.backField4"
          placeholder="请输入备用字段4"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="备用字段5" prop="backField5">
        <el-input
          v-model="queryParams.backField5"
          placeholder="请输入备用字段5"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['store:backbody:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['store:backbody:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['store:backbody:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['store:backbody:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="backbodyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="退单详情id" align="center" prop="backBodyId" />
      <el-table-column label="退单号" align="center" prop="backSerial" />
      <el-table-column label="退款金额" align="center" prop="refundAmount" />
      <el-table-column label="退单状态" align="center" prop="backStatus" />
      <el-table-column label="退单备注" align="center" prop="backProductRemarks" />
      <el-table-column label="商品编码" align="center" prop="backProductCode" />
      <el-table-column label="商品名称" align="center" prop="backProductName" />
      <el-table-column label="商品数量" align="center" prop="backProductNum" />
      <el-table-column label="商品主图" align="center" prop="backMainPic" />
      <el-table-column label="商品规格" align="center" prop="backSpecification" />
      <el-table-column label="省" align="center" prop="province" />
      <el-table-column label="市" align="center" prop="city" />
      <el-table-column label="县/区" align="center" prop="county" />
      <el-table-column label="乡村/街道" align="center" prop="village" />
      <el-table-column label="收货人" align="center" prop="addressUser" />
      <el-table-column label="联系方式" align="center" prop="addressPhone" />
      <el-table-column label="创建时间" align="center" prop="crTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.crTime, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="upTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.upTime, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备用字段1" align="center" prop="backField1" />
      <el-table-column label="备用字段2" align="center" prop="backField2" />
      <el-table-column label="备用字段3" align="center" prop="backField3" />
      <el-table-column label="备用字段4" align="center" prop="backField4" />
      <el-table-column label="备用字段5" align="center" prop="backField5" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['store:backbody:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['store:backbody:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改退单详情对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="退单号" prop="backSerial">
          <el-input v-model="form.backSerial" placeholder="请输入退单号" />
        </el-form-item>
        <el-form-item label="退款金额" prop="refundAmount">
          <el-input v-model="form.refundAmount" placeholder="请输入退款金额" />
        </el-form-item>
        <el-form-item label="退单备注" prop="backProductRemarks">
          <el-input v-model="form.backProductRemarks" placeholder="请输入退单备注" />
        </el-form-item>
        <el-form-item label="商品编码" prop="backProductCode">
          <el-input v-model="form.backProductCode" placeholder="请输入商品编码" />
        </el-form-item>
        <el-form-item label="商品名称" prop="backProductName">
          <el-input v-model="form.backProductName" placeholder="请输入商品名称" />
        </el-form-item>
        <el-form-item label="商品数量" prop="backProductNum">
          <el-input v-model="form.backProductNum" placeholder="请输入商品数量" />
        </el-form-item>
        <el-form-item label="商品主图" prop="backMainPic">
          <el-input v-model="form.backMainPic" placeholder="请输入商品主图" />
        </el-form-item>
        <el-form-item label="商品规格" prop="backSpecification">
          <el-input v-model="form.backSpecification" placeholder="请输入商品规格" />
        </el-form-item>
        <el-form-item label="省" prop="province">
          <el-input v-model="form.province" placeholder="请输入省" />
        </el-form-item>
        <el-form-item label="市" prop="city">
          <el-input v-model="form.city" placeholder="请输入市" />
        </el-form-item>
        <el-form-item label="县/区" prop="county">
          <el-input v-model="form.county" placeholder="请输入县/区" />
        </el-form-item>
        <el-form-item label="乡村/街道" prop="village">
          <el-input v-model="form.village" placeholder="请输入乡村/街道" />
        </el-form-item>
        <el-form-item label="收货人" prop="addressUser">
          <el-input v-model="form.addressUser" placeholder="请输入收货人" />
        </el-form-item>
        <el-form-item label="联系方式" prop="addressPhone">
          <el-input v-model="form.addressPhone" placeholder="请输入联系方式" />
        </el-form-item>
        <el-form-item label="逻辑删除" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入逻辑删除" />
        </el-form-item>
        <el-form-item label="创建时间" prop="crTime">
          <el-date-picker clearable
            v-model="form.crTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="更新时间" prop="upTime">
          <el-date-picker clearable
            v-model="form.upTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择更新时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备用字段1" prop="backField1">
          <el-input v-model="form.backField1" placeholder="请输入备用字段1" />
        </el-form-item>
        <el-form-item label="备用字段2" prop="backField2">
          <el-input v-model="form.backField2" placeholder="请输入备用字段2" />
        </el-form-item>
        <el-form-item label="备用字段3" prop="backField3">
          <el-input v-model="form.backField3" placeholder="请输入备用字段3" />
        </el-form-item>
        <el-form-item label="备用字段4" prop="backField4">
          <el-input v-model="form.backField4" placeholder="请输入备用字段4" />
        </el-form-item>
        <el-form-item label="备用字段5" prop="backField5">
          <el-input v-model="form.backField5" placeholder="请输入备用字段5" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBackbody, getBackbody, delBackbody, addBackbody, updateBackbody } from "@/api/store/backbody";

export default {
  name: "Backbody",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 退单详情表格数据
      backbodyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        backSerial: null,
        refundAmount: null,
        backStatus: null,
        backProductRemarks: null,
        backProductCode: null,
        backProductName: null,
        backProductNum: null,
        backMainPic: null,
        backSpecification: null,
        province: null,
        city: null,
        county: null,
        village: null,
        addressUser: null,
        addressPhone: null,
        crTime: null,
        upTime: null,
        backField1: null,
        backField2: null,
        backField3: null,
        backField4: null,
        backField5: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询退单详情列表 */
    getList() {
      this.loading = true;
      listBackbody(this.queryParams).then(response => {
        this.backbodyList = response.data;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        backBodyId: null,
        backSerial: null,
        refundAmount: null,
        backStatus: null,
        backProductRemarks: null,
        backProductCode: null,
        backProductName: null,
        backProductNum: null,
        backMainPic: null,
        backSpecification: null,
        province: null,
        city: null,
        county: null,
        village: null,
        addressUser: null,
        addressPhone: null,
        delFlag: null,
        crTime: null,
        upTime: null,
        backField1: null,
        backField2: null,
        backField3: null,
        backField4: null,
        backField5: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.backBodyId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加退单详情";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const backBodyId = row.backBodyId || this.ids
      getBackbody(backBodyId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改退单详情";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.backBodyId != null) {
            updateBackbody(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBackbody(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const backBodyIds = row.backBodyId || this.ids;
      this.$modal.confirm('是否确认删除退单详情编号为"' + backBodyIds + '"的数据项？').then(function() {
        return delBackbody(backBodyIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('store/backbody/export', {
        ...this.queryParams
      }, `backbody_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
